﻿#include "movepanel.h"
#include "ui_movepanel.h"
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>


MovePanel::MovePanel(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MovePanel)
{
    ui->setupUi(this);
    setAutoFillBackground(true);
    setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );   //设置无边框窗口,并位于最前
    setAttribute( Qt::WA_TranslucentBackground );                           //背景透明
    move(100, 100);
    setMouseTracking(true);

    //4个分块初始情况下都是这个颜色
    colors.append(QColor(135, 236, 211, alpha));                          //第二象限
    colors.append(QColor(135, 236, 211, alpha));                          //第一象限
    colors.append(QColor(135, 236, 211, alpha));                          //第三象限
    colors.append(QColor(135, 236, 211, alpha));                          //第四象限

    //4个代表区域的方块
    centerX = width()/2;
    centerY = height()/2;
    centerRect = QRect(centerX-40, centerY-40, 80, 80);         //中心区域

    initPath();
}

MovePanel::~MovePanel()
{
    delete ui;
}

void MovePanel::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    int side = qMin(width(), height());
    p.translate(width()/2, height()/2);
    p.scale(side/200.0, side/200.0);
    p.setRenderHint(QPainter::Antialiasing);



    drawBackground(p);

    drawPie(p);

    drawArrow(p);

    drawCancel(p);
//    drawPath(p);


}

void MovePanel::mouseMoveEvent(QMouseEvent *e)
{
    if(isPress){
        resetColor();                                               //把所有颜色重置
        point = e->pos();
        int currentIndex = currentPos(e->pos());
        if(currentIndex > -1){
            colors[currentIndex] = QColor(84,228,193,alpha);             //当前鼠标悬浮位置进行变色
        }

        update();
    }
}

void MovePanel::mousePressEvent(QMouseEvent *e)
{
    if(centerRect.contains(e->pos())){
        isPress = true;
    }
}

void MovePanel::mouseReleaseEvent(QMouseEvent *e)
{
    //如果选择了某一个下标，就结束
    if(currentPos(e->pos()) > -1){
        emit doSelected("");

//        close();
    }

    isPress = false;
    resetColor();
    update();
}

int MovePanel::currentPos(const QPoint &pos)
{
    for(int i=0; i<paths.count(); i++){
        if(paths.at(i).contains(pos)){
            qDebug() << "cur:" << i;
            return i;
        }
    }
    return -1;
}

void MovePanel::resetColor()
{
    for(int i=0; i<colors.count(); i++){
        colors[i]=QColor(135, 236, 211, alpha);
    }
}

void MovePanel::initPath()
{
    //构造4个扇形区
    for(int i=0; i<4; i++){
        QPainterPath path;
        //要注意，这里使用的是绝对坐标， 而不是相对side/200的坐标
        path.arcTo(QRectF(-200, -200, 400, 400), -45+(i*90), 90);
        path.translate(centerX, centerY);
        paths.append(path);
    }
}

void MovePanel::drawBackground(QPainter &p)
{
    p.save();
    p.setBrush(QColor(0, 0, 0, 0));         //绘制一个透明的背景
    p.setPen(Qt::NoPen);
    p.drawEllipse(-100, -100, 200, 200);
    p.restore();

}

void MovePanel::drawPie(QPainter &p)
{
    p.save();
    p.setPen(QColor(0, 220, 176, alpha));

    p.rotate(-45);

    //绘制第二象限
    p.setBrush(colors.at(1));
    p.drawPie(QRectF(-90, -90, 180, 180.0), 0*16, 90*16);

    //绘制第一象限
    p.rotate(-90);
    p.setBrush(colors.at(2));
    p.drawPie(QRectF(-90, -90, 180, 180.0), 0*16, 90*16);

    //绘制第三象限
    p.rotate(-90);
    p.setBrush(colors.at(3));
    p.drawPie(QRectF(-90, -90, 180, 180.0), 0*16, 90*16);

    //绘制第四象限
    p.rotate(-90);
    p.setBrush(colors.at(0));
    p.drawPie(QRectF(-90, -90, 180, 180.0), 0*16, 90*16);

    p.restore();
}

void MovePanel::drawCancel(QPainter &p)
{
    QRect rec;
    if(!isPress){
        rec = QRect(-5, -5, 10, 10);
    }else{
        rec = QRect(-2, -2, 4, 4);
    }

    p.save();
    p.setPen(QColor(28,175,244, alpha));
    p.setBrush(QColor(84,228,193,alpha));

    p.drawEllipse(rec);
    p.restore();

}

void MovePanel::drawArrow(QPainter &p)
{
    if(isPress){
        p.save();

        p.setPen(QColor(28,175,244, alpha));
        p.drawLine( 0, 0, point.x()/2-100, point.y()/2-100 );
        p.setPen(Qt::NoPen);
        p.setBrush(QColor(28,175,244, alpha));
        p.drawEllipse( point.x()/2-105, point.y()/2-105, 10, 10 );

        p.restore();
    }
}

void MovePanel::drawPath(QPainter &p)
{
    p.save();
    p.setPen(Qt::red);
    p.setBrush(Qt::yellow);
    for(int i=0; i<4; i++){
        p.drawPath(paths[i]);
    }


    p.restore();
}

